package com.xyzh05.webapp.action;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.User;
import com.xyzh05.model.UserInfo;
import com.xyzh05.service.UserInfoManager;
import com.xyzh05.service.UserManager;
import com.xyzh05.webapp.util.RequestUtil;

public class UserInfoAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = -2284478743750792883L;
	
	private UserManager userManager;
    private UserInfoManager userInfoManager;
    private List<UserInfo> userInfoList = new ArrayList<UserInfo>();
    private User user;
    private UserInfo userInfo;
    private Long id;
    private String query;
	private Integer resultCode;
	private String location;

    public void setUserInfoManager(UserInfoManager userInfoManager) {
        this.userInfoManager = userInfoManager;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String userInfoId = getRequest().getParameter("userInfo.id");
            if (userInfoId != null && !userInfoId.equals("")) {
                userInfo = userInfoManager.get(new Long(userInfoId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
    	userInfoList = userInfoManager.search(query, UserInfo.class);
        return SUCCESS;
    }

    public List<UserInfo> getUserInfoList() {
		return userInfoList;
	}

	public void setUserInfoList(List<UserInfo> userInfoList) {
		this.userInfoList = userInfoList;
	}

	public void setId(Long id) {
        this.id = id;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public User getUser() {
		return user;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}

	public Long getId() {
		return id;
	}

	public String delete() {
        userInfoManager.remove(userInfo.getId());
        saveMessage(getText("userInfo.deleted"));

        return SUCCESS;
    }

    public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String edit() {
        return SUCCESS;
    }

    public String save() throws Exception {
    	id = user.getId();
        if (id != null) {
            try {
            	User u = userManager.get(id);
            	u.setDisplayName(user.getDisplayName());
            	u.setGender(user.getGender());
            	if(u.getUserInfo() == null){
            		userInfo.setUser(u);
            		u.setUserInfo(userInfo);
            	}else{
            		u.getUserInfo().setNickName(userInfo.getNickName());   
            		
                	u.getUserInfo().setBirthYear(userInfo.getBirthYear());
                	u.getUserInfo().setBirthMonth(userInfo.getBirthMonth());
                	u.getUserInfo().setBirthDay(userInfo.getBirthDay());
                	
                	u.getUserInfo().setProvince(userInfo.getProvince());
                	u.getUserInfo().setCity(userInfo.getCity());
                	u.getUserInfo().setCounty(userInfo.getCounty());
            	}            
                userManager.save(u);
                resultCode = 1;
			} catch (Exception e) {
	        	resultCode = 0;
			}
        } else {
        	resultCode = 0;
        }
        return SUCCESS;
    }
    
    /**
     * 设置位置到Cookie中
     */
    public String saveLocation(){
        try {
        	if("home".equals(location)){
        		RequestUtil.setCookie(getResponse(), "cookieLocation", URLEncoder.encode("住宅","utf-8"), "locationPath");        		
        	}else if ("office".equals(location)){
        		RequestUtil.setCookie(getResponse(), "cookieLocation", URLEncoder.encode("办公室","utf-8"), "locationPath");
        	}else{
        		RequestUtil.setCookie(getResponse(), "cookieLocation", location, "locationPath");
        	}
            RequestUtil.setCookie(getResponse(), "cookieAddress", URLEncoder.encode(userInfo.getAddress(),"utf-8"), "locationPath");
            RequestUtil.setCookie(getResponse(), "cookieProvince", URLEncoder.encode(userInfo.getProvince(),"utf-8"), "locationPath");
            RequestUtil.setCookie(getResponse(), "cookieCity", URLEncoder.encode(userInfo.getCity(),"utf-8"), "locationPath");
            RequestUtil.setCookie(getResponse(), "cookieCounty", URLEncoder.encode(userInfo.getCounty(),"utf-8"), "locationPath");
            resultCode = 1;
		} catch (Exception e) {
			e.printStackTrace();
        	resultCode = 0;
		}
        return SUCCESS;
    }
}